Vue.directive('clickoutside', {
  bind: function (el, binding, vnode) {
    function documentHandler(e) {
      // 修饰符
      if (binding.modifiers.esc) {
        if (e.keyCode == 27) {
          binding.value(e)
        }
      }
      if (el.contains(e.target)) {
        return false
      }
      if (binding.expression) {
        binding.value(e)
      }
    }
    el.__vueClickOutside__ = documentHandler
    document.addEventListener('click', documentHandler)
    document.addEventListener('keyup', documentHandler)
  },
  unbind: function (el, binding) {
    document.removeEventListener('click', el.__vueClickOutside__)
    document.removeEventListener('keyup', el.__vueClickOutside__)
    delete el.__vueClickOutside__
  }
})